package com.commerceerp.system.service;

import com.commerceerp.system.entity.ShopWish;
import com.commerceerp.system.mapper.ShopMapper;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.StringUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/5/13/013.
 */
@Service
public class TokenService {

    @Autowired
    private ShopMapper shopMapper;

    public ApiResult saveToken(String auth_code) {
        if(StringUtils.isNotEmpty(auth_code)){
            StringBuffer url = new StringBuffer("https://merchant.wish.com/api/v2/oauth/access_token?");
            url.append("client_id=5b4c54860fa3691b043c5d8d");
            url.append("&client_secret=98082705a4594933aac4fe6e8fae546b");
            url.append("&code="+auth_code);
            url.append("&redirect_uri=https://jv.mind-erp.com/wish/authorization_code");
            url.append("&grant_type=authorization_code");
            String result = StringUtils.httpURLConnectionPOST(url.toString(),null);
            if(StringUtils.isEmpty(result)){
                return new ApiResult(405,"请求token失败");
            }
            JSONObject json = JSONObject.fromObject(result);
            int code = json.getInt("code");
            if(code == 0){
                JSONObject data = json.optJSONObject("data");//
                ShopWish shopWish = new ShopWish();
                String access_token = data.getString("access_token");
                String expiry_string = data.getString("expiry_string");
                String client_id = data.getString("client_id");
                shopWish.setToken(access_token);
                shopWish.setCode(auth_code);
                shopWish.setAnthTime(expiry_string);
                shopWish.setClientId(client_id);
                shopWish.setDelFlag("0");
                shopWish.update();
                shopMapper.updWishToken(shopWish);
                return new ApiResult(200,"授权成功");
            }
            return new ApiResult(405,"没有找到对应的店铺");
        }
        return new ApiResult(405,"code是空的");
    }

    //验证门店是否授权
    public ApiResult vailCode(String clientId) {
        //根据clientId获取wish门店
        ShopWish shopWish = shopMapper.getShopWishByClientId(clientId);
        if(shopWish == null){
            return new ApiResult(405,"没有找到对应的店铺");
        }
        if(StringUtils.isEmpty(shopWish.getToken())){
            shopMapper.delWishByClientId(clientId);
            return new ApiResult(405,"授权失败");
        }
        String result = StringUtils.httpURLConnectionPOST("https://merchant.wish.com/api/v2/auth_test?access_token="+shopWish.getToken(),null);
        if(StringUtils.isEmpty(result)){
            return new ApiResult(405,"");
        }
        JSONObject json = JSONObject.fromObject(result);
        int code = json.getInt("code");
        if(code != 0){
            return new ApiResult(405,"授权码失效或无效，请重新验证");
        }
        return new ApiResult(200,"授权成功");
    }
}
